#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1
PROG=/usr/bin/opengpt

get_config() {
	config_get_bool enabled $1 enabled 0
	config_get_bool tb_enable $1 tb_enable 0
	config_get level $1 level "info"
	config_get host $1 host "0.0.0.0"
	config_get port $1 port "7999"
	config_get workers $1 workers '1'
	config_get proxies $1 proxies ''
	config_get timeout $1 timeout '600'
	config_get connect_timeout $1 connect_timeout '60'
	config_get tcp_keepalive $1 tcp_keepalive "5"
	config_get tls_cert $1 tls_cert ''
	config_get tls_key $1 tls_key ''
	config_get arkose_har_path $1 arkose_har_path ''
	config_get arkose_har_upload_key $1 arkose_har_upload_key ''
	config_get arkose_yescaptcha_key $1 arkose_yescaptcha_key ''
	config_get arkose_token_endpoint $1 arkose_token_endpoint ''
	config_get sign_secret_key $1 sign_secret_key ''
	config_get tb_store_strategy $1 tb_store_strategy "mem"
	config_get tb_redis_url $1 tb_redis_url 'redis://127.0.0.1:6379'
	config_get tb_capacity $1 tb_capacity "60"
	config_get tb_fill_rate $1 tb_fill_rate "1"
	config_get tb_expired $1 tb_expired "86400"
}

start_service() {
	config_load opengpt
	config_foreach get_config opengpt
	[ $enabled -ne 1 ] && return 1

    args=""
    
	if [ -n "$level" ]; then
		args="$args --level $level"
	fi

	if [ -n "$host" ]; then
		args="$args --host $host"
	fi

	if [ -n "$port" ]; then
		args="$args --port $port"
	fi

	if [ -n "$workers" ]; then
		args="$args --workers $workers"
	fi

	if [ -n "$proxies" ]; then
		args="$args --proxies $proxies"
	fi

	if [ -n "$timeout" ]; then
		args="$args --timeout $timeout"
	fi

	if [ -n "$connect_timeout" ]; then
		args="$args --connect-timeout $connect_timeout"
	fi

	if [ -n "$tcp_keepalive" ]; then
		args="$args --tcp-keepalive $tcp_keepalive"
	fi

	if [ -n "$arkose_har_path" ]; then
		args="$args --arkose-har-path $arkose_har_path"
	fi
	
	if [ -n "$arkose_har_upload_key" ]; then
		args="$args --arkose-har-upload-key $arkose_har_upload_key"
	fi
	
	if [ -n "$arkose_yescaptcha_key" ]; then
		args="$args --arkose-yescaptcha-key $arkose_yescaptcha_key"
	fi

	if [ -n "$arkose_token_endpoint" ]; then
		args="$args --arkose-token-endpoint $arkose_token_endpoint"
	fi

	if [ -n "$tls_cert" ]; then
		args="$args --tls-cert $tls_cert"
	fi

	if [ -n "$tls_key" ]; then
		args="$args --tls-key $tls_key"
	fi

	if [ -n "$sign_secret_key" ]; then
		args="$args --sign-secret-key $sign_secret_key"
	fi

	if [ $tb_enable -ne 0 ]; then 
		args="$args --tb-enable"
		if [ -n "$tb_redis_url" ]; then
		    args="$args --tb-redis-url $tb_redis_url"
        fi
		if [ -n "$tb_store_strategy" ]; then
		    args="$args --tb-store-strategy $tb_store_strategy"
        fi
		if [ -n "$tb_capacity" ]; then
		    args="$args --tb-capacity $tb_capacity"
        fi
		if [ -n "$tb_fill_rate" ]; then
		    args="$args --tb-fill-rate $tb_fill_rate"
        fi
		if [ -n "$tb_expired" ]; then
		    args="$args --tb-expired $tb_expired"
        fi
	fi
	
	procd_open_instance
	procd_set_param command /bin/sh -c "$PROG serve run $args >>/var/log/opengpt.log 2>&1"
	procd_set_param stdout 0
	procd_set_param stderr 0
	procd_set_param pidfile /var/run/opengpt.pid
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "opengpt"
}

reload_service() {
	stop
	for i in $(seq 1 60); do
		if [ -z "$(ps -ef | grep opengpt | grep -v grep | grep -v $$ | grep -v '/etc/rc.common' | grep -v '/sbin/service' | grep -v '/sbin/luci-reload')" ]; then
			break
		fi
		sleep 1
	done
	start
}